package com.e0box.game.utils.storage
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.system.ApplicationDomain;
	
	import utils.ObjectArray;

	public class ApplicationStorage
	{
		protected static var _instance:ApplicationStorage;
		
		/**
		 * the storage(ObjectArr).
		 */		
		protected var _storage:ObjectArray;
		
		public function ApplicationStorage(single:validate)
		{
			if(!single){
				throw new Error("Please use the static method 'get instance'.");
			}
			init();
		}
		
		private function init():void{
			_storage = new ObjectArray();
		}
		
		/**
		 * getter the storage ObjectArray.
		 * don't use it
		 * 
		 * @return 
		 * 
		 */		
		public function get storage():ObjectArray{
			return _storage;
		}
		/**
		 * put BitmapData in storage with the key
		 * 
		 * @param key
		 * @param value
		 * 
		 */		
		public function put(key:String,value:Class):void{
			_storage.put(key,value);
		}
		/**
		 * get BitmapData by key
		 * return null if storage don't contain this key
		 * @param key
		 * @return 
		 * 
		 */		
		public function getDefinitionByKey(key:String):Class{
			return _storage.getValueByKey(key) as Class;
			
		}
		
		public function getInstanceByKey(key:String):Object{
			var cla:Class = getDefinitionByKey(key);
			if(cla){
				var ins:Object = new cla();
				if(ins is BitmapData){
					return new Bitmap(ins as BitmapData);
				}
				return ins;
			}else if(BitmapDataStorage.instance.containsKey(key)){
				return new Bitmap(BitmapDataStorage.instance.getBitmapByKey(key));
			}else if(ApplicationDomain.currentDomain.hasDefinition(key)){				
				var ins2:Object = new (ApplicationDomain.currentDomain.getDefinition(key) as Class)();
				
				if(ins2 is BitmapData){
					return new Bitmap(ins2 as BitmapData);
				}
				return ins2;
			}
			return null;
		}
		
		/**
		 * getter count of storage
		 * @return 
		 * 
		 */		
		public function get size():uint{
			return _storage.size;
		}
		/**
		 * clear the storage.dispose all of the BitmapData
		 * 
		 */		
		public function clear():void{
			_storage.clear();
		}
		/**
		 * check storage contain the key
		 * 
		 * @param key
		 * @return 
		 * 
		 */		
		public function containsKey(key:String):Boolean{
			return _storage.containsKey(key);
		}
		/**
		 * remove & dispose BitmapData by key
		 * @param key
		 * @return 
		 * 
		 */		
		public function removeByKey(key:String):Boolean{
			return _storage.removeByKey(key);
		}
		/**
		 * get all of storage keys
		 * 
		 * @return 
		 * 
		 */		
		public function getKeys():Vector.<String>{
			var result:Vector.<String>=Vector.<String>(_storage.getKeys());
			
			return result;
		}
		/**
		 * get all of storage BitmapDatas
		 * @return 
		 * 
		 */		
		public function getValues():Vector.<Class>{
			var result:Vector.<Class>=Vector.<Class>(_storage.getValues());
			
			return result;
		}
		
		
		/**
		 * only way to get instance of this class.
		 * @return 
		 * 
		 */		
		public static function get instance():ApplicationStorage{
			if(!_instance){
				_instance=new ApplicationStorage(new validate());
			}
			return _instance;
		}
	}
}
class validate{
	public function validate(){
		
	}
}